/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workforce.limousine.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.workforce.util.Utils;
import org.workforce.vo.LabelValue;

/**
 *
 * @author krisada
 */
public class SearchLookup {

    private DataSource dataSource = null;

    public SearchLookup() {
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public List<LabelValue> find(Object params[]) {
        String groupId = (String)params[0];
        List<LabelValue> list = new ArrayList<LabelValue>();
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            StringBuilder s = new StringBuilder();
            s.append("SELECT IFNULL(LOOKUP_NAME, '-') AS LOOKUP_NAME, IFNULL(LOOKUP_VALUE, '-') AS LOOKUP_VALUE FROM lookup WHERE LOOKUP_GROUP_ID = ? ORDER BY LOOKUP_ORDER");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement stmt = conn.prepareStatement(s.toString());
            stmt.setString(1, groupId);
            ResultSet rs = stmt.executeQuery();
            list.add(new LabelValue("-1","PLEASE SELECT"));
            while (rs.next()) {
                LabelValue lookup = new LabelValue();
                lookup.setText(Utils.StringField(rs.getString(1)));
                lookup.setValue(rs.getString(2));
                list.add(lookup);
            }
            rs.close();
            rs = null;
            stmt.close();
            stmt = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                    conn = null;
                } catch (Exception ex) {
                }
            }
        }
        return list;
    }
}
